limsSample.receivePage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-sample-receive-list-grid',
            url: limsSample.batchSampleUrl + '/select/samples',
            requestData: {
                status_EQ: 'receive',
                createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            onCellClick: function (field, row) {
                if (field === 'ext$.batchcode' && row.ext$.batchid) {
                    Gikam.loadBatchDetailPageV12(row.ext$.batchid, true)
                } else if (field === 'sampleCode' && row.id) {
                    limsSample.loadSampleDetailTopPage(row, true)
                }
            },
            columns: limsSample.getSampleReceiveListColumns(),
            page: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData(['sample-receive-auditrecord-grid']);
                    Gikam.getAsyncComp('sample-receive-uploader').done(function (comp) {
                        comp.setOptions({
                            bizId: -1,
                        });
                    });
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('sample-receive-uploader').done(function (comp) {
                    comp.setOptions({
                        bizId: row.id,
                    });
                });
                Gikam.getAsyncComp('sample-receive-auditrecord-grid').done(function (grid) {
                    grid.refresh({
                        url: limsSample.auditRecordUrl + '/queries',
                        requestData: {
                            businessId_SEQ: row.id,
                            businessCode_SEQ: 'T_LIMS_SAMPLE'
                        }
                    });
                });
            },
            toolbar: [{
                type: 'button',
                text: 'SAMPLE.MODLE.BUTTON.RECEIVE_SAMPLE',
                icon: 'edit',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-receive-list-grid');
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    var idList = selections.map(function (item) {
                        return item.id
                    });
                    var model = Gikam.create('modal', {
                        title: 'SAMPLE.MODLE.BUTTON.RECEIVE_SAMPLE',
                        width: 330,
                        height: 240,
                    });
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'sample-receive-form',
                                columns: 1,
                                fields: limsSample.getSampleReceiveFormFields(),
                                autoSave: false,
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('sample-receive-form');
                                        if (form.validate()) {
                                            var data = form.getData();
                                            var chooseData = selections.map(e => {
                                                return {
                                                    id: e.id,
                                                    receiveStatus: data.receiveStatus,
                                                    receiveAbnormalType: Gikam.isEmpty(data.receiveAbnormalType) ? '' : data.receiveAbnormalType,
                                                    receiveAbnormalRemark: Gikam.isEmpty(data.receiveAbnormalRemark) ? '' : data.receiveAbnormalRemark
                                                }
                                            })
                                            workspace.window.showMask()
                                            Gikam.put(limsSample.baseUrl + '/instant', Gikam.getJsonWrapper(null, [null, chooseData])).done(function () {
                                                Gikam.getComp('lims-sample-receive-list-grid').refreshRowById(idList);
                                            }).always(function () {
                                                workspace.window.closeMask();
                                                Gikam.getLastModal().close(data);
                                            })
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close();
                                    }
                                }]
                            }]
                        }
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.ASSIGN',
                icon: 'edit',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-receive-list-grid');
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }

                    var idList = selections.map(function (item) {
                        return item.id
                    });
                    let ids = idList.join(',');
                    Gikam.create('modal', {
                        width: '90%',
                        height: '90%',
                        title: Gikam.choose.getConfig('sample-board-choose').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("sample-board-choose").url + Gikam.param({
                            id_IN: ids
                        }),
                        onAfterClose: function () {
                            Gikam.getComp('lims-sample-receive-list-grid').refreshRowById(idList);
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-receive-list-grid');
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    if (grid.validate(selections)) {
                        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                            let url = limsSample.baseUrl + '/receive/submit'
                            Gikam.postText(url, Gikam.getJsonWrapper(null, [null, selections])).done(function (req) {
                                grid.refresh()
                            })
                        })
                    }
                }
            }, {
                type: 'button',
                text: 'SAMPLE-ABNORMAL.BUTTON.TIP.UPLOAD-ATTACH-MULTI',
                icon: 'add',
                onClick: function () {
                    _this.multiUploadAttachments()
                }
            }, {
                type: 'button',
                text: 'SAMPLE_CHECK.BUTTON.COURIER_SEARCH',
                icon: 'search',
                onClick: function () {
                    _this.courierSearch()
                }
            }, {
                type: 'form',
                id: 'warp-form',
                fields: [{
                    field: 'sampleCode_CISC',
                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                }],
            }, {
                type: 'form',
                id: 'courier-search-form',
                fields: [{
                    field: 'courierNumber_CISC',
                    title: 'T_LIMS_SAMPLE.COURIERNUMBER',
                }],
            },],
        }
    },

    multiUploadAttachments: function () {
        var _this = this
        var grid = Gikam.getComp('lims-sample-receive-list-grid')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        var bizId = selections[0].id
        Gikam.create('simpleUploader', {
            id: 'simple-uploader',
            dbTable: 'T_LIMS_SAMPLE',
            bizCategory: 'receive',
            bizId: bizId,
            onAfterClose: function (fileList) {
                if (fileList.length === 0) {
                    return
                }
                var successFiles = fileList.filter(r => {
                    return r.state === 'success'
                })
                var fileIds = successFiles.map(f => f.id).join(',')
                var url = limsSample.baseUrl + '/upload-attachment-multi'
                Gikam.postText(url, Gikam.getJsonWrapper({
                    bizCategory: 'receive', fileIds: fileIds
                }, [null, selections])).done(function (req) {
                    grid.refresh()
                })
            }
        })
    },

    courierSearch: function () {
        var grid = Gikam.getComp('lims-sample-receive-list-grid')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        var courierData = []
        selections.forEach(r => {
            var flag = true
            courierData.forEach(c => {
                if (c.courierNumber === r.courierNumber && c.courier === r.courier) {
                    flag = false
                }
            })
            if (flag) {
                courierData.push({courierNumber: r.courierNumber, courier: r.courier, id: r.id})
            }
        })
        var model = Gikam.create('modal', {
            title: 'SAMPLE_CHECK.BUTTON.COURIER_SEARCH',
            height: '90%',
            width: '90%',
        });
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            west: {
                width: '25%',
                items: [{
                    type: 'grid',
                    id: 'courier-number-grid',
                    columns: [{
                        field: 'courierNumber',
                        title: 'T_LIMS_SAMPLE.COURIERNUMBER',
                        width: 140,
                    }, {
                        field: 'courier',
                        title: 'T_LIMS_SAMPLE.COURIER',
                        type: 'select',
                        category: 'sys',
                        param: {
                            codeCategoryId: 'courier'
                        },
                    }],
                    onRendered: function () {
                        if (courierData.length > 0) {
                            console.log(courierData)
                            Gikam.getComp('courier-number-grid').loadData(courierData)
                            this.activeRowByIndex(0)
                        }
                    },
                    onRowActive: function (index, row) {
                        Gikam.postText(limsSample.baseUrl + '/get-courierdata', Gikam.getJsonWrapper({
                            courier: row.courier,
                            courierNumber: row.courierNumber,
                            id: row.id
                        }, [null, []])).done(req => {
                            req = JSON.parse(req)
                            Gikam.getAsyncComp('courier-search-grid').done(grid => {
                                grid.loadData(req.rows)
                            })
                        })
                    },
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.CLOSE',
                        icon: 'cancel',
                        onClick: function () {
                            Gikam.getLastModal().close()
                        }
                    }]
                }]
            },
            center: {
                items: [{
                    type: 'grid',
                    id: 'courier-search-grid',
                    columns: [{
                        field: 'time',
                        type: 'dateTime',
                        title: '时间',
                        width: 140,
                    }, {
                        field: 'location',
                        title: '地点',
                        width: 140,
                    }, {
                        field: 'describe',
                        title: '描述',
                    }],
                    onRendered: function () {
                    },
                }]
            }
        })
    },

    getSubTabParam: function () {
        var _this = this;
        return {
            type: 'tab',
            fill: true,
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'sample-receive-uploader',
                    dbTable: 'T_LIMS_SAMPLE',
                    onBeforeUpload: function () {
                        var activedRow = Gikam.getComp('lims-sample-receive-list-grid').getActivedRow()
                        if (Gikam.isEmpty(activedRow)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                            return false
                        }
                    },
                    formatterGridColumns: function (columns) {
                        columns.splice(2, 0, {
                            field: 'bizCategory',
                            title: 'LIMS.MODLE.FILE.FILETYPE',
                            type: 'select',
                            width: 200,
                            category: 'sys',
                            param: {
                                codeCategoryId: 'sampleFileType'
                            }
                        });
                        return Gikam.getFormatterGridColumns(columns, true);
                    },
                    formatterGridToolbar: function (toolbar) {
                        toolbar.splice(0, 1, {
                            type: 'button',
                            text: 'GIKAM.FILE.BUTTON.UPLOAD_FILE',
                            icon: 'upload',
                            onClick: function () {
                                let batchGrid = Gikam.getComp('lims-sample-receive-list-grid');
                                let activedRow = batchGrid.getActivedRow();
                                if (Gikam.isEmpty(activedRow)) {
                                    Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                                    return;
                                }
                                Gikam.create('simpleUploader', {
                                    dbTable: 'T_LIMS_SAMPLE',
                                    bizId: activedRow.id,
                                    multiple: false,
                                    autoClose: true,
                                    bizCategory: 'receive',
                                    onAfterClose: function (fileList) {
                                        if (Gikam.isEmpty(fileList)) {
                                            return;
                                        }
                                        Gikam.getComp('sample-receive-uploader').setOptions({
                                            bizId: activedRow.id
                                        })
                                    }
                                });
                            }
                        });
                        return toolbar;
                    }
                }]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [{
                    type: 'grid',
                    id: 'sample-receive-auditrecord-grid',
                    columns: limsSample.getFlowListColumns()
                }]
            }]
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-sample-receive-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [_this.getGridParam()]
            },
            center: {
                items: [_this.getSubTabParam()]
            }
        });
    },

    init: function () {
        this.create()
    }
};
